package com.hsmw.api.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hsmw.api.service.HttpAPIUtils;
import com.hsmw.api.service.HttpClientUtil;
import com.hsmw.api.vo.YLYNewOrderDTO;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.constant.OperTypeEnum;
import com.hsmw.common.util.MapUtil;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.HsmwConcreteInfo;
import com.hsmw.domain.vo.GpsInfoEntity;
import com.qcloud.Utilities.MD5;
import com.qcloud.cos.utils.Md5Utils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Md5Crypt;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class YLWUtils {
    public static final String YLW_URL= "https://open-api.10ss.net/print/index";

    public static final String YLW_CALCEL_URL= "https://open-api.10ss.net/printer/cancelall";

    public static final String YLW_PIC_CALCEL_URL= "https://open-api.10ss.net/printer/deleteicon";




    //发灰提醒
    public static HashMap sendStartOrPasuse(YLYNewOrderDTO orderDTO ){

        String  url = YLW_URL;
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        HashMap param = new HashMap();
        HashMap signMap =getSignAndID();
        param.put("client_id",CommonConstants.YLW_CLIENT_ID);
        param.put("access_token",CommonConstants.YLW_ACCESS_TOKEN);
        param.put("machine_code",orderDTO.getMechineId());
        param.put("origin_id","123123123123");
        param.put("sign",signMap.get("sign"));
        param.put("id",signMap.get("id"));
        param.put("timestamp",signMap.get("timestamp"));
        StringBuilder  content  = new StringBuilder();
        content.append("<center>请登录混天凝APP，查询更多信息哦</center>").append("\\r");
        if(OperTypeEnum.START.getValue().equals(orderDTO.getOperType())){
            content.append("<FB><FH2>").append("发灰提醒:").append("</FH2></FB>").append("\\r").append("\\r");
            content.append("<FH>"+orderDTO.getItemName()+"要求开始发灰</FH>").append("\\r").append("\\r");
            content.append("<FH>发灰要求</FH>").append("\\r");
            content.append("<FH>"+orderDTO.getConcreteNum()+"-------"+orderDTO.getSlump()+"-------"+orderDTO.getExpectVolume()+"方</FH>").append("\\r");
        }
        if(OperTypeEnum.ENDED.getValue().equals(orderDTO.getOperType())){
            content.append("<FB><FH2>").append("订单结束提醒:").append("</FH2></FB>").append("\\r").append("\\r");
            content.append("<FH>"+orderDTO.getItemName()+"结束发灰</FH>").append("\\r").append("\\r");
            content.append("<FH>本次订单已经完成，请登录混天凝APP提交本次订单方量信息。</FH>").append("\\r").append("\\r");
        }
        content.append("<FH>订单信息---------------------</FH>").append("\\r");
        content.append("<FH>订单编号:").append("\\r").append(orderDTO.getOrderId()+"</FH>").append("\\r");
        content.append("<FH>订单联系人:").append("\\r").append(orderDTO.getForemanName()+orderDTO.getForemanTel()+"</FH>").append("\\r");
        content.append("<FH>预计送灰时间:").append("\\r").append(orderDTO.getReachTime()+"</FH>").append("\\r");
        content.append("<FH>施工单位:").append("\\r").append(orderDTO.getConstructionUnitName()+"</FH>").append("\\r");
        content.append("<FH>项目位置:").append("\\r").append(orderDTO.getItemPosition()+"</FH>").append("\\r");
        content.append("<FH>具体混凝土需求:").append("\\r").append("\\r");
        if(CollectionUtil.isNotEmpty(orderDTO.getHsmwConcreteInfos())){
            for(HsmwConcreteInfo concreteInfo :orderDTO.getHsmwConcreteInfos()){
                content.append("<FH>标号:"+concreteInfo.getConcreteNum());
                if (StringUtils.isNotBlank(concreteInfo.getSlump())){
                    content.append("、坍落度:"+concreteInfo.getSlump());
                }
                if(StringUtils.isNotBlank(concreteInfo.getIncreaseandOrDecrease())){
                    content.append("±"+concreteInfo.getIncreaseandOrDecrease());
                }
                content.append("、方量："+concreteInfo.getExpectVolume()+"</FH>").append("\\r");
            }
        }
        content.append("\\r").append("\\r");

        param.put("content",content.toString());
        String result=null;
        try {
            System.out.println(url.toString());
            result =   HttpAPIUtils.doPost(url.toString(),param);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap results  = new HashMap();
        Double travel_mileage = 0.0;
        Double total_time = 0.0;
        if(StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            travel_mileage = res.getDouble("travel_mileage");
            total_time = res.getDouble("total_time");
        }
        System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        results.put("travelMileage",travel_mileage);
        results.put("total_time",total_time);
        return results;
    }




    public static HashMap sendNewOrder(YLYNewOrderDTO orderDTO ){

        String  url = YLW_URL;
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        HashMap param = new HashMap();
        /* 新订单提醒:
        订单编号:HTN0809898707
        订单发起人:张三13212345678项目名称:国展二期
        预计送灰时间:5月2日 13:30施工单位:中建八局
        项目位置:天津市滨海新区新北路与东海北路交叉路口
        具体混凝土需求:
        标号:C40坍落度:120+140、方量:30m3
        标号:C30、坍落度:120+140、方量:30m3
        标号:C60、坍落度:120+140、方量:30m3*/

        HashMap signMap =getSignAndID();
        param.put("client_id",CommonConstants.YLW_CLIENT_ID);
        param.put("access_token",CommonConstants.YLW_ACCESS_TOKEN);
        param.put("machine_code",orderDTO.getMechineId());
        param.put("origin_id","123123123123");
        param.put("sign",signMap.get("sign"));
        param.put("id",signMap.get("id"));
        param.put("timestamp",signMap.get("timestamp"));
        StringBuilder  content  = new StringBuilder();
        //content.append("<ML>1</ML>");
        content.append("<center><FB><FH2>").append("新订单提醒:").append("</FH2></FB></center>").append("\\r");
        content.append("<FH>您有新的订单，请登录混天凝APP查看订单信息。</FH>").append("\\r").append("\\r");
       /* content.append("<FH>订单发起人:").append("\\r").append(orderDTO.getForemanName()+orderDTO.getForemanTel()+"</FH>").append("\\r");
        content.append("<FH>项目名称:").append("\\r").append(orderDTO.getItemName()+"</FH>").append("\\r");
        content.append("<FH>预计送灰时间:").append("\\r").append(orderDTO.getReachTime()+"</FH>").append("\\r");
        content.append("<FH>施工单位:").append("\\r").append(orderDTO.getConstructionUnitName()+"</FH>").append("\\r");
        content.append("<FH>项目位置:").append("\\r").append(orderDTO.getItemPosition()+"</FH>").append("\\r");
        content.append("<FH>具体混凝土需求:").append("\\r").append("\\r");
        if(CollectionUtil.isNotEmpty(orderDTO.getHsmwConcreteInfos())){
            for(HsmwConcreteInfo concreteInfo :orderDTO.getHsmwConcreteInfos()){
                content.append("<FH>标号:"+concreteInfo.getConcreteNum()+"、坍落度:"+concreteInfo.getSlump()+"、方量："+concreteInfo.getExpectVolume()+"</FH>").append("\\r");
            }
        }
        content.append("\\r").append("\\r");
        if(CollectionUtil.isNotEmpty(orderDTO.getPumpReqList())){
            content.append("<FH>泵车需求:</FH>").append("\\r");
            for(String str :orderDTO.getPumpReqList()){
                content.append("<FH>"+str+"</FH>").append("\\r");
            }
        }*/
        param.put("content",content.toString());
        String result=null;
        try {
            System.out.println(url.toString());
            result =   HttpAPIUtils.doPost(url.toString(),param);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap results  = new HashMap();
        Double travel_mileage = 0.0;
        Double total_time = 0.0;
        if(StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            travel_mileage = res.getDouble("travel_mileage");
            total_time = res.getDouble("total_time");
        }
        System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        results.put("travelMileage",travel_mileage);
        results.put("total_time",total_time);
        return results;
    }

    public static HashMap ensureNewOrder(YLYNewOrderDTO orderDTO ){

        String  url = YLW_URL;
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        HashMap param = new HashMap();
        /* 新订单提醒:
        订单编号:HTN0809898707
        订单发起人:张三13212345678项目名称:国展二期
        预计送灰时间:5月2日 13:30施工单位:中建八局
        项目位置:天津市滨海新区新北路与东海北路交叉路口
        具体混凝土需求:
        标号:C40坍落度:120+140、方量:30m3
        标号:C30、坍落度:120+140、方量:30m3
        标号:C60、坍落度:120+140、方量:30m3*/

        HashMap signMap =getSignAndID();
        param.put("client_id",CommonConstants.YLW_CLIENT_ID);
        param.put("access_token",CommonConstants.YLW_ACCESS_TOKEN);
        param.put("machine_code",orderDTO.getMechineId());
        param.put("origin_id","123123123123");
        param.put("sign",signMap.get("sign"));
        param.put("id",signMap.get("id"));
        param.put("timestamp",signMap.get("timestamp"));
        StringBuilder  content  = new StringBuilder();
        //content.append("<ML>1</ML>");
        content.append("<center>请登录混天凝APP，查询更多信息哦</center>").append("\\r");
        content.append("<center><FB><FH2>").append("新订单提醒:").append("</FH2></FB></center>").append("\\r");
        content.append("<FH>订单编号:").append("\\r").append(orderDTO.getOrderId()+"</FH>").append("\\r");
        content.append("<FH>订单联系人:").append("\\r").append(orderDTO.getForemanName()+orderDTO.getForemanTel()+"</FH>").append("\\r");
        content.append("<FH>项目名称:").append("\\r").append(orderDTO.getItemName()+"</FH>").append("\\r");
        content.append("<FH>预计送灰时间:").append("\\r").append(orderDTO.getReachTime()+"</FH>").append("\\r");
        content.append("<FH>施工单位:").append("\\r").append(orderDTO.getConstructionUnitName()+"</FH>").append("\\r");
        content.append("<FH>项目位置:").append("\\r").append(orderDTO.getItemPosition()+"</FH>").append("\\r");
        content.append("<FH>具体混凝土需求:").append("\\r").append("\\r");
        if(CollectionUtil.isNotEmpty(orderDTO.getHsmwConcreteInfos())){
            for(HsmwConcreteInfo concreteInfo :orderDTO.getHsmwConcreteInfos()){
                content.append("<FH>标号:"+concreteInfo.getConcreteNum());
                if (StringUtils.isNotBlank(concreteInfo.getSlump())){
                    content.append("、坍落度:"+concreteInfo.getSlump());
                }
                if(StringUtils.isNotBlank(concreteInfo.getIncreaseandOrDecrease())){
                    content.append("±"+concreteInfo.getIncreaseandOrDecrease());
                }
                content.append("、方量："+concreteInfo.getExpectVolume()+"</FH>").append("\\r");
            }
        }
        content.append("\\r").append("\\r");
     /*   if(CollectionUtil.isNotEmpty(orderDTO.getPumpReqList())){
            content.append("<FH>泵车需求:</FH>").append("\\r");
            for(String str :orderDTO.getPumpReqList()){
                content.append("<FH>"+str+"</FH>").append("\\r");
            }
        }*/
        param.put("content",content.toString());
        String result=null;
        try {
            System.out.println(url.toString());
            result =   HttpAPIUtils.doPost(url.toString(),param);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap results  = new HashMap();
        Double travel_mileage = 0.0;
        Double total_time = 0.0;
        if(StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            travel_mileage = res.getDouble("travel_mileage");
            total_time = res.getDouble("total_time");
        }
        System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        results.put("travelMileage",travel_mileage);
        results.put("total_time",total_time);
        return results;
    }




    public static HashMap sendCancelOrder(YLYNewOrderDTO orderDTO ){

        String  url = YLW_URL;
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        HashMap param = new HashMap();
        /* 新订单提醒:
        订单编号:HTN0809898707
        订单发起人:张三13212345678项目名称:国展二期
        预计送灰时间:5月2日 13:30施工单位:中建八局
        项目位置:天津市滨海新区新北路与东海北路交叉路口
        具体混凝土需求:
        标号:C40坍落度:120+140、方量:30m3
        标号:C30、坍落度:120+140、方量:30m3
        标号:C60、坍落度:120+140、方量:30m3*/

        HashMap signMap =getSignAndID();
        param.put("client_id",CommonConstants.YLW_CLIENT_ID);
        param.put("access_token",CommonConstants.YLW_ACCESS_TOKEN);
        param.put("machine_code",orderDTO.getMechineId());
        param.put("origin_id","123123123123");
        param.put("sign",signMap.get("sign"));
        param.put("id",signMap.get("id"));
        param.put("timestamp",signMap.get("timestamp"));
        StringBuilder  content  = new StringBuilder();
        //content.append("<ML>1</ML>");
        content.append("<center>登录混天凝APP查看订单信息</center>").append("\\r");
        content.append("<center><FB><FH2>").append("订单取消提醒:").append("</FH2></FB></center>").append("\\r");
        content.append("<FH>以下订单已被工长取消</FH>").append("\\r").append("\\r");
        content.append("<FH>订单编号:").append("\\r").append(orderDTO.getOrderId()+"</FH>").append("\\r");
        content.append("<FH>订单发起人:").append("\\r").append(orderDTO.getForemanName()+orderDTO.getForemanTel()+"</FH>").append("\\r");
        content.append("<FH>项目名称:").append("\\r").append(orderDTO.getItemName()+"</FH>").append("\\r");
        content.append("<FH>预计送灰时间:").append("\\r").append(orderDTO.getReachTime()+"</FH>").append("\\r");
        content.append("<FH>施工单位:").append("\\r").append(orderDTO.getConstructionUnitName()+"</FH>").append("\\r");
        content.append("<FH>项目位置:").append("\\r").append(orderDTO.getItemPosition()+"</FH>").append("\\r");
        param.put("content",content.toString());
        String result=null;
        try {
            System.out.println(url.toString());
            result =   HttpAPIUtils.doPost(url.toString(),param);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap results  = new HashMap();
        Double travel_mileage = 0.0;
        Double total_time = 0.0;
        if(StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            travel_mileage = res.getDouble("travel_mileage");
            total_time = res.getDouble("total_time");
        }
        System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        results.put("travelMileage",travel_mileage);
        results.put("total_time",total_time);
        return results;
    }

    public static HashMap cancelAllCall(){

        String  url = YLW_CALCEL_URL;
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        HashMap param = new HashMap();
        /* 新订单提醒:
        订单编号:HTN0809898707
        订单发起人:张三13212345678项目名称:国展二期
        预计送灰时间:5月2日 13:30施工单位:中建八局
        项目位置:天津市滨海新区新北路与东海北路交叉路口
        具体混凝土需求:
        标号:C40坍落度:120+140、方量:30m3
        标号:C30、坍落度:120+140、方量:30m3
        标号:C60、坍落度:120+140、方量:30m3*/

        HashMap signMap =getSignAndID();
        param.put("client_id",CommonConstants.YLW_CLIENT_ID);
        param.put("access_token",CommonConstants.YLW_ACCESS_TOKEN);
        param.put("machine_code",CommonConstants.YLW_MACHINE_CODE);
        param.put("origin_id","123123123123");
        param.put("sign",signMap.get("sign"));
        param.put("id",signMap.get("id"));
        param.put("timestamp",signMap.get("timestamp"));
        String result=null;
        try {
            System.out.println(url.toString());
            result =   HttpAPIUtils.doPost(url.toString(),param);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap results  = new HashMap();
        Double travel_mileage = 0.0;
        Double total_time = 0.0;
        if(StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            travel_mileage = res.getDouble("travel_mileage");
            total_time = res.getDouble("total_time");
        }
        System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        results.put("travelMileage",travel_mileage);
        results.put("total_time",total_time);
        return results;
    }


    public static HashMap cancelPicAllCall(){

        String  url = YLW_PIC_CALCEL_URL;
        long begin = System.currentTimeMillis();
        System.out.println("开始时间===="+begin);
        HashMap param = new HashMap();
        /* 新订单提醒:
        订单编号:HTN0809898707
        订单发起人:张三13212345678项目名称:国展二期
        预计送灰时间:5月2日 13:30施工单位:中建八局
        项目位置:天津市滨海新区新北路与东海北路交叉路口
        具体混凝土需求:
        标号:C40坍落度:120+140、方量:30m3
        标号:C30、坍落度:120+140、方量:30m3
        标号:C60、坍落度:120+140、方量:30m3*/

        HashMap signMap =getSignAndID();
        param.put("client_id",CommonConstants.YLW_CLIENT_ID);
        param.put("access_token",CommonConstants.YLW_ACCESS_TOKEN);
        param.put("machine_code",CommonConstants.YLW_MACHINE_CODE);
        param.put("origin_id","123123123123");
        param.put("sign",signMap.get("sign"));
        param.put("id",signMap.get("id"));
        param.put("timestamp",signMap.get("timestamp"));
        String result=null;
        try {
            System.out.println(url.toString());
            result =   HttpAPIUtils.doPost(url.toString(),param);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap results  = new HashMap();
        Double travel_mileage = 0.0;
        Double total_time = 0.0;
        if(StringUtils.isNotBlank(result)) {
            JSONObject res = JSON.parseObject(result);
            travel_mileage = res.getDouble("travel_mileage");
            total_time = res.getDouble("total_time");
        }
        System.out.println("麦谷请求gps需时间："+(System.currentTimeMillis()-begin));
        results.put("travelMileage",travel_mileage);
        results.put("total_time",total_time);
        return results;
    }


    public static void main(String[] args) {
        cancelPicAllCall();
    }


    public static   HashMap getSignAndID(){
        Long currentTime = System.currentTimeMillis();
        //将client_id+timestamp+client_secret(三个参数只需要值),再做md5,得出字符串转成小写即为sign
        currentTime= currentTime/1000;
        String md5Str =CommonConstants.YLW_CLIENT_ID+currentTime +CommonConstants.YLW_CLIENT_SECRET;
        String  sign =Md5Utils.md5Hex(md5Str.getBytes());
        HashMap result = new HashMap();
        result.put("sign",sign);
        result.put("id", UUID.randomUUID());
        result.put("timestamp",currentTime);
        return  result;
    }


}
